var express = require('express')
var connection = require('../db/db_utill');

var router = express.Router()

//首页路由
router.post('/', function (req, res) {
  const body = req.body
  connection.query(`SELECT * FROM topic_info where topic_category=? and topic_state=0`, [body.topic_category], function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results.length) {
      var string = JSON.stringify(results);
      var topic_info = JSON.parse(string);
      res.status(200).json({
        status: 200,
        //话题数据
        topic_info
      })
    } else {
      res.json({
        message: '获取数据失败！'
      })
    }
  });
})

//查询前10话题
router.post('/top10', function (req, res) {
  connection.query(`SELECT topic_title,topic_id FROM topic_info order by topic_agreeNum  DESC limit 5`, function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results.length) {
      var string = JSON.stringify(results);
      var topic_info = JSON.parse(string);
      res.json({
        status: 200,
        //话题数据
        topic_info
      })
    } else {
      res.json({
        message: '获取数据失败！'
      })
    }
  });
})

module.exports = router;